package com.tansun.easycare.rule.ruleproj.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.rule.common.DataCache;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.ResMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.datamodel.service.DataModelService;
import com.tansun.easycare.rule.ruleproj.entity.RuleProjectRank;
import com.tansun.easycare.rule.ruleproj.service.RuleProjectRankService;

/**
 * 层级结构管理Controller
 * 
 * @author lph
 * @version 2019-09-11
 */
@RestController
@RequestMapping(value = "/ruleproj/ruleProjectRank")
public class RuleProjectRankController extends RuleBaseController {

	@Autowired
	private RuleProjectRankService ruleProjectRankService;

	@Autowired
	private DataModelService dataModelService;

	@RequestMapping(value = "save")
	public ResponseVO save(@RequestBody RuleProjectRank ruleProjectRank) {
		ResponseVO ret = new ResponseVO();
		try {
			ruleProjectRank.setIsenable("1");
			ruleProjectRankService.save(ruleProjectRank);
			//将层级id返回到前端
			ret.setDesDetail(ruleProjectRank.getId());
		} catch (Exception e) {
			e.printStackTrace();
			ret.setDescription(e.getMessage());
			;
			ret.setStatus(ResponseVO.ERROR);
		}
		return ret;
	}

	// 新增引用查询，确认是否删除
	@RequestMapping(value = "referenceQuery")
	public ResponseVO referenceQuery(String tidArr) {
		ResponseVO ret = new ResponseVO();
		try {
			if (tidArr != null) {
				String msg = ruleProjectRankService.findRefCount(tidArr);
				ret.setDesDetail(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.setDescription(e.getMessage());
			ret.setStatus(ResponseVO.ERROR);
		}
		return ret;
	}

	@RequestMapping(value = "delete")
	public ResponseVO delete(String tidArr, String projectCode) {
		ResponseVO ret = new ResponseVO();
		try {
			ruleProjectRankService.deleteRankInfo(tidArr, projectCode);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setDescription(e.getMessage());;
			ret.setStatus(ResponseVO.ERROR);
		}
		return ret;
	}

	@RequestMapping(value = "getRankList")
	public @ResponseBody JsonMessage getRankList(String projectCode) {
		JsonMessage ret = new JsonMessage();
		try {
			RuleProjectRank rankCondition = new RuleProjectRank();
			rankCondition.setProjectCode(projectCode);
			List<RuleProjectRank> ranklist = ruleProjectRankService.findList(rankCondition);
			ret.setData(JSON.toJSONString(ranklist));
		} catch (Exception e) {
			ret.setResult(false);
			ret.setMessage(e.getMessage());
		}
		return ret;
	}

	/**
	 * 移动
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "move", produces = "application/json; charset=utf-8")
	public ResponseVO move(String ids,String projectCode, String nodeId, String moveType) throws Exception {
		ResponseVO ret=new ResponseVO();
		ResMessage msg = null;
		try {
			if (projectCode == null || "".equals(projectCode)) {
				return ret;
			}
			msg = this.dataModelService.move(ids, projectCode, nodeId, moveType);
			DataCache.paramChange(projectCode);
			ret.setDescription(msg.getMsg());
			ret.setDesDetail(msg.getCode());
			ret.setStatus(200);
		} catch (Exception e) {
			logger.debug(ExceptionUtils.getStackTrace(e));
			ret.setDescription(msg.getMsg());
			ret.setStatus(ResponseVO.ERROR);
		}

		return ret;
	}

	/**
	 * 移动ztree页面信息
	 * 
	 * @param projectCode
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("queryRankInfo")
	public ResponseVO queryRankInfo(String projectCode) throws Exception {
		ResponseVO ret = new ResponseVO();
		try {
			if (projectCode == null || "".equals(projectCode)) {
				return ret;
			}
			RuleProjectRank rankCondition = new RuleProjectRank();
			rankCondition.setProjectCode(projectCode);
			rankCondition.setModuleType("file");
			List<RuleProjectRank> ranklist = ruleProjectRankService.findList(rankCondition);
			// 过滤本身
			List<RuleProjectRank> finalRanklist = new ArrayList();
			for (RuleProjectRank dataset : ranklist) {
				if (!"ruleDeploy".equals(dataset.getNodeId()) && !"ruleApproval".equals(dataset.getNodeId())) {
					finalRanklist.add(dataset);
				}
			}
			ret.setData(finalRanklist);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setDescription(e.getMessage());
			ret.setStatus(ResponseVO.ERROR);
		}
		return ret;
	}
	/**
	 * 移动ztree页面信息
	 * 
	 * @param projectCode
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("queryRankInfoByType")
	public ResponseVO queryRankInfoByType(String projectCode, String type) throws Exception {
		ResponseVO ret = new ResponseVO();
		try {
			if (projectCode == null || "".equals(projectCode)) {
				return ret;
			}
			RuleProjectRank rankCondition = new RuleProjectRank();
			rankCondition.setProjectCode(projectCode);
			List<RuleProjectRank> ranklist = ruleProjectRankService.findList(rankCondition);
			// 过滤本身
			Map<String,RuleProjectRank> rankMap = new HashMap<String,RuleProjectRank>();
			List<RuleProjectRank> finalRanklist = new ArrayList();
			for (RuleProjectRank dataset : ranklist) {
				if (dataset.getModuleType().equals(type)) {
//					finalRanklist.add(dataset);
					finalRanklist.add(dataset);
					findParent(dataset,ranklist,rankMap,projectCode);
				}
			}
			for(Entry<String, RuleProjectRank> entry : rankMap.entrySet()) {
				finalRanklist.add(entry.getValue());
			}
			ret.setData(finalRanklist);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setDescription(e.getMessage());
			ret.setStatus(ResponseVO.ERROR);
		}
		return ret;
	}
	private void findParent(RuleProjectRank curr,List<RuleProjectRank> ranklist,Map<String,RuleProjectRank> resultMap,String projejctCode) {
		for (RuleProjectRank rank : ranklist) {
			if(curr.getPId().equals(rank.getNodeId())) {
				resultMap.put(rank.getId(), rank);
				if(!rank.getNodeId().equals(projejctCode)) {
					findParent(rank,ranklist,resultMap,projejctCode);
				}
			}
			
		}
	}
	
	
}